/*********************************************************************
 *                                                                   *
 *  Js Css Minifier ( JsCssMin )                                     *
 *  Author: Igor Kanshyn                                             *
 *  Copyright (c) 2009, Igor Kanshyn. All rights reserved.           *
 *                                                                   *
 *  JsCssMin is released under the Apache Software License.          *
 *  See LICENSE.txt for more details.                                *
 *                                                                   *
 *********************************************************************/

package org.grenader.jscssmin;

import static junit.framework.Assert.assertEquals;
import org.grenader.jscssmin.model.ResourceBlock;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;

/**
 * <p>Product: JsCssMin ( JavaScript and CSS Minifier )<br>
 * Author: Igor Kanshyn (grenader).
 * </p>
 * Date: Apr 25, 2009
 */
public class JCSSMinTestCase {

    @Test
    public void testGetBlockContent_JavaScript() {
        ResourceBlock block = new ResourceBlock();
        block.add(ResourceUtils.createResource("<script src=\"js1.js\" type=\"text/javascript\"></script>"));
        block.add(ResourceUtils.createResource("<script src=\"js2.js\" type=\"text/javascript\"></script>"));

        block.processBlock(new EmptyJsCssProcessor());
        String resText = block.getBlockContent("4");
        assertEquals("<script type=\"text/javascript\" src=\"js1_js2_v4.js\"></script>\n", resText);
    }

    @Test
    public void testGenerateFullContent_CSS() {
        EmptyJsCssProcessor processor = new EmptyJsCssProcessor();
        processor.getConfig().setVersion("2");

        JsCssMin jsCssMin = new JsCssMin("111\n" +
                "<!-- JsCssMin Package Start -->\n" +
                "<link rel=\"stylesheet\" href=\"my1.css\" type=\"text/css\" />\n" +
                "<link rel=\"stylesheet\" href=\"my2.css\" type=\"text/css\" />\n" +
                "<!-- JsCssMin Package End -->\n" +
                "222", processor);

        jsCssMin.process();
        String resText = jsCssMin.getContent();
        assertEquals("111\n" +
                "<!-- JsCssMin Package Start -->\n" +
                "<link type=\"text/css\" rel=\"stylesheet\" href=\"my1_my2_v2.css\"/>\n" +
                "<!-- JsCssMin Package End -->\n" +
                "222\n", resText);
    }

    @Test
    public void testGenerateFullContent_skipRepetiotion() {
        EmptyJsCssProcessor processor = new EmptyJsCssProcessor();
        processor.getConfig().setVersion("2");
        processor.getConfig().setSkipRepetition(true);

        JsCssMin jsCssMin = new JsCssMin("111\n" +
                "<!-- JsCssMin Package Start -->\n" +
                "<link rel=\"stylesheet\" href=\"my1.css\" type=\"text/css\" />\n" +
                "<link rel=\"stylesheet\" href=\"my1.css\" type=\"text/css\" />\n" +
                "<!-- JsCssMin Package End -->\n" +
                "222", processor);

        jsCssMin.process();
        String resText = jsCssMin.getContent();
        assertEquals("111\n" +
                "<!-- JsCssMin Package Start -->\n" +
                "<link type=\"text/css\" rel=\"stylesheet\" href=\"my1_v2.css\"/>\n" +
                "<!-- JsCssMin Package End -->\n" +
                "222\n", resText);
    }

    @Test
    public void testGenerateFullContent_withRepetition() {
        EmptyJsCssProcessor processor = new EmptyJsCssProcessor();
        processor.getConfig().setVersion("2");
        processor.getConfig().setSkipRepetition(false);

        JsCssMin jsCssMin = new JsCssMin("111\n" +
                "<!-- JsCssMin Package Start -->\n" +
                "<link rel=\"stylesheet\" href=\"my1.css\" type=\"text/css\" />\n" +
                "<link rel=\"stylesheet\" href=\"my1.css\" type=\"text/css\" />\n" +
                "<!-- JsCssMin Package End -->\n" +
                "222", processor);

        jsCssMin.process();
        String resText = jsCssMin.getContent();
        assertEquals("111\n" +
                "<!-- JsCssMin Package Start -->\n" +
                "<link type=\"text/css\" rel=\"stylesheet\" href=\"my1_my1_v2.css\"/>\n" +
                "<!-- JsCssMin Package End -->\n" +
                "222\n", resText);
    }

    @Test
    public void testGenerateFullContent_mixedContent() {
        EmptyJsCssProcessor processor = new EmptyJsCssProcessor();
        processor.getConfig().setVersion("2");
        processor.getConfig().setSkipRepetition(false);

        JsCssMin jsCssMin = new JsCssMin("111\n" +
                "<!-- JsCssMin Package Start -->\n" +
                "<link rel=\"stylesheet\" href=\"my1.css\" type=\"text/css\" />\n" +
                "<script src=\"js2.js\" type=\"text/javascript\"></script>\n" +
                "<!-- JsCssMin Package End -->\n" +
                "222", processor);

        jsCssMin.process();

        String resText = jsCssMin.getContent();
        assertEquals("111\n" +
                "<!-- JsCssMin Package Start -->\n" +
                "<link type=\"text/css\" rel=\"stylesheet\" href=\"my1_v2.css\"/>\n" +
                "<script type=\"text/javascript\" src=\"js2_v2.js\"></script>\n" +
                "<!-- JsCssMin Package End -->\n" +
                "222\n", resText);
    }

    @Test
    public void testGetBlockContent_aFileNotFound() {
        FileNotFoundJsCssProcessor processor = new FileNotFoundJsCssProcessor("no-file.js");
        processor.getConfig().setVersion("2");

        JsCssMin jsCssMin = new JsCssMin("111\n" +
                "<!-- JsCssMin Package Start -->\n" +
                "<script src=\"js1.js\" type=\"text/javascript\"></script>\n" +
                "<script src=\"no-file.js\" type=\"text/javascript\"></script>\n" +
                "<script src=\"js2.js\" type=\"text/javascript\"></script>\n" +
                "<!-- JsCssMin Package End -->\n" +
                "222", processor);

        jsCssMin.process();
        assertEquals(2, jsCssMin.getBlockCount());

        String resText = jsCssMin.getContent();
        assertEquals("111\n" +
                "<!-- JsCssMin Package Start -->\n" +
                "<script type=\"text/javascript\" src=\"js1_v2.js\"></script>\n" +
                "<script src=\"no-file.js\" type=\"text/javascript\"></script>\n" +
                "<script type=\"text/javascript\" src=\"js2_v2.js\"></script>\n" +
                "<!-- JsCssMin Package End -->\n" +
                "222\n", resText);

    }


    @Test
    public void testGetBlockContent_aFileNotFound2() {
        FileNotFoundJsCssProcessor processor = new FileNotFoundJsCssProcessor("no-file.js");
        processor.getConfig().setVersion("2");

        JsCssMin jsCssMin = new JsCssMin("111\n" +
                "<!-- JsCssMin Package Start -->\n" +
                "<script src=\"js1.js\" type=\"text/javascript\"></script>\n" +
                "<script src=\"js3.js\" type=\"text/javascript\"></script>\n" +
                "<script src=\"no-file.js\" type=\"text/javascript\"></script>\n" +
                "<script src=\"js2.js\" type=\"text/javascript\"></script>\n" +
                "<!-- JsCssMin Package End -->\n" +
                "222", processor);

        jsCssMin.process();
        assertEquals(2, jsCssMin.getBlockCount());

        String resText = jsCssMin.getContent();
        assertEquals("111\n" +
                "<!-- JsCssMin Package Start -->\n" +
                "<script type=\"text/javascript\" src=\"js1_js3_v2.js\"></script>\n" +
                "<script src=\"no-file.js\" type=\"text/javascript\"></script>\n" +
                "<script type=\"text/javascript\" src=\"js2_v2.js\"></script>\n" +
                "<!-- JsCssMin Package End -->\n" +
                "222\n", resText);

    }

    @Test
    public void testIsJSSMinStart_Empty() {
        JsCssMin jsCssMin = new JsCssMin(null);

        assertFalse(jsCssMin.isJCSSMinStart(null));
        assertFalse(jsCssMin.isJCSSMinStart(""));
    }

    @Test
    public void testIsJSSMinStart_noComments() {
        JsCssMin jsCssMin = new JsCssMin("non-required-text");

        assertFalse(jsCssMin.isJCSSMinStart("wdw ewwer"));
        assertFalse(jsCssMin.isJCSSMinStart("<!--"));
        assertFalse(jsCssMin.isJCSSMinStart("<!-- ->"));
        assertFalse(jsCssMin.isJCSSMinStart("<!- -->"));
        assertFalse(jsCssMin.isJCSSMinStart("<!- wedwed -->"));
        assertFalse(jsCssMin.isJCSSMinStart("<!---->"));
    }

    @Test
    public void testIsJSSMinEnd_noComments() {
        JsCssMin jsCssMin = new JsCssMin("non-required-text");

        assertFalse(jsCssMin.isJCSSMinEnd("wdw ewwer"));
        assertFalse(jsCssMin.isJCSSMinEnd("<!--"));
        assertFalse(jsCssMin.isJCSSMinEnd("<!-- ->"));
        assertFalse(jsCssMin.isJCSSMinEnd("<!- -->"));
        assertFalse(jsCssMin.isJCSSMinEnd("<!- wedwed -->"));
        assertFalse(jsCssMin.isJCSSMinEnd("<!---->"));
    }

    @Test
    public void testIsJSSMinStart_false() {
        JsCssMin jsCssMin = new JsCssMin("non-required-text");

        assertFalse(jsCssMin.isJCSSMinStart("<!--  JSSeMin Package Start -->"));
        assertFalse(jsCssMin.isJCSSMinStart("<!--  sd sdf dJCSSMin Pawckage Start -->"));
        assertFalse(jsCssMin.isJCSSMinStart("<!-- JsCssMin.  sdcs  Package  Start. er freffe--> reref"));
    }

    @Test
    public void testIsJSSMinStart_true() {
        JsCssMin jsCssMin = new JsCssMin("non-required-text");

        assertTrue(jsCssMin.isJCSSMinStart("<!--  JsCssMin Package Start -->"));
        assertTrue(jsCssMin.isJCSSMinStart("<!--  sd sdf dJsCssMin Package Start -->"));
        assertTrue(jsCssMin.isJCSSMinStart("<!-- JsCssMin.  sdcs  Package Start. er freffe--> reref"));
    }

    @Test
    public void testIsJSSMinEnd_false() {
        JsCssMin jsCssMin = new JsCssMin("non-required-text");

        assertFalse(jsCssMin.isJCSSMinEnd("<!--  JSSeMin Package End -->"));
        assertFalse(jsCssMin.isJCSSMinEnd("<!--  sd sdf dJsCssMin Pawckage End -->"));
        assertFalse(jsCssMin.isJCSSMinEnd("<!-- JsCssMin.  sdcs  Package  End. er freffe--> reref"));
    }

    @Test
    public void testIsJSSMinEnd_true() {
        JsCssMin jsCssMin = new JsCssMin("non-required-text");

        assertTrue(jsCssMin.isJCSSMinEnd("<!--  JsCssMin Package End -->"));
        assertTrue(jsCssMin.isJCSSMinEnd("<!--  sd sdf dJsCssMin Package End -->"));
        assertTrue(jsCssMin.isJCSSMinEnd("<!-- JsCssMin.  sdcs  Package End. er freffe--> reref"));
    }

}
